<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head><meta name="generator" content="Hexo 3.8.0">
  <meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform">
<meta http-equiv="Cache-Control" content="no-siteapp">
















  
  
  <link href="/limh/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css">







<link href="/limh/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css">

<link href="/limh/css/main.css?v=5.1.4" rel="stylesheet" type="text/css">


  <link rel="apple-touch-icon" sizes="180x180" href="/limh/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/limh/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/limh/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/limh/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="教程,">










<meta name="description" content="我们学习串口通信主要是要实现单片机和电脑之间的信息交互，可以用电脑控制单片机的一些信息，可以把单片机的一些信息状况发给电脑上的软件。下面我们就做一个简单的例程，实现单片机串口调试助手发送的数据，在我们开发板上的数码管上显示出来。 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647">
<meta name="keywords" content="教程">
<meta property="og:type" content="article">
<meta property="og:title" content="11.6、通信实例与ASCII码">
<meta property="og:url" content="https://tdnr.gitee.io/limh/2014/12/01/mcu075/index.html">
<meta property="og:site_name" content="土逗牛肉">
<meta property="og:description" content="我们学习串口通信主要是要实现单片机和电脑之间的信息交互，可以用电脑控制单片机的一些信息，可以把单片机的一些信息状况发给电脑上的软件。下面我们就做一个简单的例程，实现单片机串口调试助手发送的数据，在我们开发板上的数码管上显示出来。 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="http://c.biancheng.net/cpp/uploads/allimg/140318/1-14031Q6234Q96.png">
<meta property="og:image" content="http://c.biancheng.net/cpp/uploads/allimg/140318/1-14031Q62503637.png">
<meta property="og:image" content="http://c.biancheng.net/cpp/uploads/allimg/140318/1-14031Q6255AB.png">
<meta property="og:updated_time" content="2019-01-27T06:37:55.248Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="11.6、通信实例与ASCII码">
<meta name="twitter:description" content="我们学习串口通信主要是要实现单片机和电脑之间的信息交互，可以用电脑控制单片机的一些信息，可以把单片机的一些信息状况发给电脑上的软件。下面我们就做一个简单的例程，实现单片机串口调试助手发送的数据，在我们开发板上的数码管上显示出来。 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647">
<meta name="twitter:image" content="http://c.biancheng.net/cpp/uploads/allimg/140318/1-14031Q6234Q96.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/limh/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://tdnr.gitee.io/limh/2014/12/01/mcu075/">





  <title>11.6、通信实例与ASCII码 | 土逗牛肉</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans ">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/limh/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">土逗牛肉</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/limh/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br>
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/limh/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br>
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/limh/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br>
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/limh/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br>
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/limh/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br>
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://tdnr.gitee.io/limh/limh/2014/12/01/mcu075/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="土逗牛肉">
      <meta itemprop="description" content>
      <meta itemprop="image" content="/limh/images/avatar.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="土逗牛肉">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">11.6、通信实例与ASCII码</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2014-12-01T20:36:58+08:00">
                2014-12-01
              </time>
            

            

            
          </span>

          
            <span class="post-category">
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/limh/categories/单片机教程/" itemprop="url" rel="index">
                    <span itemprop="name">单片机教程</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>我们学习串口通信主要是要实现单片机和电脑之间的信息交互，可以用电脑控制单片机的一些信息，可以把单片机的一些信息状况发给电脑上的软件。下面我们就做一个简单的例程，实现单片机串口调试助手发送的数据，在我们开发板上的数码管上显示出来。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br></pre></td><td class="code"><pre><span class="line">#include &lt;reg52.h&gt;</span><br><span class="line">sbit ADDR3 = P1^3;</span><br><span class="line">sbit ENLED = P1^4;</span><br><span class="line">unsigned char code LedChar[] = &#123; //数码管显示字符转换表</span><br><span class="line">    0xC0, 0xF9, 0xA4, 0xB0, 0x99, 0x92, 0x82, 0xF8,</span><br><span class="line">    0x80, 0x90, 0x88, 0x83, 0xC6, 0xA1, 0x86, 0x8E</span><br><span class="line">&#125;;</span><br><span class="line">unsigned char LedBuff[7] = &#123; //数码管+独立 LED 显示缓冲区</span><br><span class="line">    0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF</span><br><span class="line">&#125;;</span><br><span class="line">unsigned char T0RH = 0; //T0 重载值的高字节</span><br><span class="line">unsigned char T0RL = 0; //T0 重载值的低字节</span><br><span class="line">unsigned char RxdByte = 0; //串口接收到的字节</span><br><span class="line">void ConfigTimer0(unsigned int ms);</span><br><span class="line">void ConfigUART(unsigned int baud);</span><br><span class="line">void main()&#123;</span><br><span class="line">    EA = 1; //使能总中断</span><br><span class="line">    ENLED = 0; //选择数码管和独立 LED</span><br><span class="line">    ADDR3 = 1;</span><br><span class="line">    ConfigTimer0(1); //配置 T0 定时 1ms</span><br><span class="line">    ConfigUART(9600); //配置波特率为 9600</span><br><span class="line">    while (1)&#123; //将接收字节在数码管上以十六进制形式显示出来</span><br><span class="line">        LedBuff[0] = LedChar[RxdByte &amp; 0x0F];</span><br><span class="line">        LedBuff[1] = LedChar[RxdByte &gt;&gt; 4];</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* 配置并启动 T0，ms-T0 定时时间 */</span><br><span class="line">void ConfigTimer0(unsigned int ms)&#123;</span><br><span class="line">    unsigned long tmp; //临时变量</span><br><span class="line">    tmp = 11059200 / 12; //定时器计数频率</span><br><span class="line">    tmp = (tmp * ms) / 1000; //计算所需的计数值</span><br><span class="line">    tmp = 65536 - tmp; //计算定时器重载值</span><br><span class="line">    tmp = tmp + 13; //补偿中断响应延时造成的误差</span><br><span class="line">    T0RH = (unsigned char)(tmp&gt;&gt;8); //定时器重载值拆分为高低字节</span><br><span class="line">    T0RL = (unsigned char)tmp;</span><br><span class="line">    TMOD &amp;= 0xF0; //清零 T0 的控制位</span><br><span class="line">    TMOD |= 0x01; //配置 T0 为模式 1</span><br><span class="line">    TH0 = T0RH; //加载 T0 重载值</span><br><span class="line">    TL0 = T0RL;</span><br><span class="line">    ET0 = 1; //使能 T0 中断</span><br><span class="line">    TR0 = 1; //启动 T0</span><br><span class="line">&#125;</span><br><span class="line">/* 串口配置函数，baud-通信波特率 */</span><br><span class="line">void ConfigUART(unsigned int baud)&#123;</span><br><span class="line">    SCON = 0x50; //配置串口为模式 1</span><br><span class="line">    TMOD &amp;= 0x0F; //清零 T1 的控制位</span><br><span class="line">    TMOD |= 0x20; //配置 T1 为模式 2</span><br><span class="line">    TH1 = 256 - (11059200/12/32)/baud; //计算 T1 重载值</span><br><span class="line">    TL1 = TH1; //初值等于重载值</span><br><span class="line">    ET1 = 0; //禁止 T1 中断</span><br><span class="line">    ES = 1; //使能串口中断</span><br><span class="line">    TR1 = 1; //启动 T1</span><br><span class="line">&#125;</span><br><span class="line">/* LED 动态扫描刷新函数，需在定时中断中调用 */</span><br><span class="line">void LedScan()&#123;</span><br><span class="line">    static unsigned char i = 0; //动态扫描索引</span><br><span class="line">    P0 = 0xFF; //关闭所有段选位，显示消隐</span><br><span class="line">    P1 = (P1 &amp; 0xF8) | i; //位选索引值赋值到 P1 口低 3 位</span><br><span class="line">    P0 = LedBuff[i]; //缓冲区中索引位置的数据送到 P0 口</span><br><span class="line">    if (i &lt; 6)&#123; //索引递增循环，遍历整个缓冲区</span><br><span class="line">        i++;</span><br><span class="line">    &#125;else&#123;</span><br><span class="line">        i = 0;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">/* T0 中断服务函数，完成 LED 扫描 */</span><br><span class="line">void InterruptTimer0() interrupt 1&#123;</span><br><span class="line">    TH0 = T0RH; //重新加载重载值</span><br><span class="line">    TL0 = T0RL;</span><br><span class="line">    LedScan(); //LED 扫描显示</span><br><span class="line">&#125;</span><br><span class="line">/* UART 中断服务函数 */</span><br><span class="line">void InterruptUART() interrupt 4&#123;</span><br><span class="line">    if (RI)&#123; //接收到字节</span><br><span class="line">        RI = 0; //手动清零接收中断标志位</span><br><span class="line">        RxdByte = SBUF; //接收到的数据保存到接收字节变量中</span><br><span class="line">        //接收到的数据又直接发回，叫作-&quot;echo&quot;，</span><br><span class="line">        //用以提示用户输入的信息是否已正确接收</span><br><span class="line">        SBUF = RxdByte;</span><br><span class="line">    &#125;</span><br><span class="line">    if (TI)&#123; //字节发送完毕</span><br><span class="line">        TI = 0; //手动清零发送中断标志位</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>大家在做这个实验的时候，有个小问题要注意一下。因为 STC89C52 下载程序是使用了UART 串口下载，下载完程序后，程序运行起来了，可是下载软件最后还会通过串口发送一些额外的数据，所以程序刚下载进去不是显示 00，而可能是其他数据。大家只要把电源开关关闭，重新打开一次就好了。</p>
<p>细心的同学可能会发现，在串口调试助手发送选项和接收选项处，还有个“字符格式发送”和“字符格式显示”，这是什么意思呢？</p>
<p>先抛开我们使用的汉字不谈，那么我们常用的字符就包含了 0~9 的数字、A~Z/a~z 的字母、还有各种标点符号等。那么在单片机系统里面我们怎么来表示它们呢？ ASCII 码（American Standard Code for Information Interchange，即美国信息互换标准代码）可以完成这个使命：我们知道，在单片机中一个字节的数据可以有 0～255 共 256 个值，我们取其中的 0～127 共 128 个值赋予了它另外一层涵义，即让它们分别来代表一个常用字符，其具体的对应关系如表 11-3 所示。</p>
<p><strong>表 11-3 ASCII 码字符表</strong></p>
<table><tbody><tr><th>ASC</th><th>控制</th><th>ASCII</th><th>字符</th><th>ASCII</th><th>字符</th><th>ASCII</th><th>字符</th></tr><tr><td>000<br>值</td><td>NUL<br>字符</td><td>032<br>值</td><td>(space)</td><td>064<br>值</td><td>@</td><td>096<br>值</td><td>‟</td></tr><tr><td>001</td><td>SOH</td><td>033</td><td>!</td><td>065</td><td>A</td><td>097</td><td>a</td></tr><tr><td>002</td><td>STX</td><td>034</td><td>“</td><td>066</td><td>B</td><td>098</td><td>b</td></tr><tr><td>003</td><td>ETX</td><td>035</td><td>#</td><td>067</td><td>C</td><td>099</td><td>c</td></tr><tr><td>004</td><td>EOT</td><td>036</td><td>$</td><td>068</td><td>D</td><td>100</td><td>d</td></tr><tr><td>005</td><td>END</td><td>037</td><td>%</td><td>069</td><td>E</td><td>101</td><td>e</td></tr><tr><td>006</td><td>ACK</td><td>038</td><td>&amp;</td><td>070</td><td>F</td><td>102</td><td>f</td></tr><tr><td>007</td><td>BEL</td><td>039</td><td>‘</td><td>071</td><td>G</td><td>103</td><td>g</td></tr><tr><td>008</td><td>BS</td><td>040</td><td>(</td><td>072</td><td>H</td><td>104</td><td>h</td></tr><tr><td>009</td><td>HT</td><td>041</td><td>)</td><td>073</td><td>I</td><td>105</td><td>i</td></tr><tr><td>010</td><td>LF</td><td>042</td><td>*</td><td>074</td><td>J</td><td>106</td><td>j</td></tr><tr><td>011</td><td>VT</td><td>043</td><td>+</td><td>075</td><td>K</td><td>107</td><td>k</td></tr><tr><td>012</td><td>FF</td><td>044</td><td>，</td><td>076</td><td>L</td><td>108</td><td>l</td></tr><tr><td>013</td><td>CR</td><td>045</td><td>-</td><td>077</td><td>M</td><td>109</td><td>m</td></tr><tr><td>014</td><td>SO</td><td>046</td><td>．</td><td>078</td><td>N</td><td>110</td><td>n</td></tr><tr><td>015</td><td>SI</td><td>047</td><td>/</td><td>079</td><td>O</td><td>111</td><td>o</td></tr><tr><td>016</td><td>DLE</td><td>048</td><td>0</td><td>080</td><td>P</td><td>112</td><td>p</td></tr><tr><td>017</td><td>DC1</td><td>049</td><td>1</td><td>081</td><td>Q</td><td>113</td><td>q</td></tr><tr><td>018</td><td>DC2</td><td>050</td><td>2</td><td>082</td><td>R</td><td>114</td><td>r</td></tr><tr><td>019</td><td>DC3</td><td>051</td><td>3</td><td>083</td><td>S</td><td>115</td><td>s</td></tr><tr><td>020</td><td>DC4</td><td>052</td><td>4</td><td>084</td><td>T</td><td>116</td><td>t</td></tr><tr><td>021</td><td>NAK</td><td>053</td><td>5</td><td>085</td><td>U</td><td>117</td><td>u</td></tr><tr><td>022</td><td>SYN</td><td>054</td><td>6</td><td>086</td><td>V</td><td>118</td><td>v</td></tr><tr><td>023</td><td>ETB</td><td>055</td><td>7</td><td>087</td><td>W</td><td>119</td><td>w</td></tr><tr><td>024</td><td>CAN</td><td>056</td><td>8</td><td>088</td><td>X</td><td>120</td><td>x</td></tr><tr><td>025</td><td>EM</td><td>057</td><td>9</td><td>089</td><td>Y</td><td>121</td><td>y</td></tr><tr><td>026</td><td>SUB</td><td>058</td><td>:</td><td>090</td><td>Z</td><td>122</td><td>z</td></tr><tr><td>027</td><td>ESC</td><td>059</td><td>;</td><td>091</td><td>[</td><td>123</td><td>{</td></tr><tr><td>028</td><td>FS</td><td>060</td><td>&lt;</td><td>092</td><td>\</td><td>124</td><td>¦</td></tr><tr><td>029</td><td>GS</td><td>061</td><td>=</td><td>093</td><td>]</td><td>125</td><td>}</td></tr><tr><td>030</td><td>RS</td><td>062</td><td>&gt;</td><td>094</td><td>^</td><td>126</td><td>~</td></tr><tr><td>031</td><td>US</td><td>063</td><td>?</td><td>095</td><td>_</td><td>127</td><td>DEL</td></tr></tbody></table>

<p>这样我们就在常用字符和字节数据之间建立了一一对应的关系，那么现在一个字节就既可以代表一个整数又可以代表一个字符了，但它本质上只是一个字节的数据，而我们赋予了它不同的涵义，什么时候赋予它哪种涵义就看编程者的意图了。ASCII 码在单片机系统中应用非常广泛，我们后续的课程也会经常使用到它，下面我们来对它做一个直观的认识，同学们一定要深刻理解其本质。</p>
<p>对照上述表格，我们就可以实现字符和数字之间的转换了，比如还是这个程序，我们发送的时候改成字符格式发送，接收还是用十六进制接收，这样接收和数码管好做一下对比。</p>
<p>我们用字符格式发送一个小写的 a，返回一个十六进制的 0x61，数码管上显示的也是 61，ASCII 码表里字符 a 对应十进制是 97，等于十六进制的 0x61；我们再用字符格式发送一个数字 1，返回一个十六进制的 0x31，数码管上显示的也是 31，ASCII 表里字符 1 对应的十进制是 49，等于十六进制的 0x31。这下大家就该清楚了：所谓的十六进制发送和十六进制接收，都是按字节数据的真实值进行的；而字符格式发送和字符格式接收，是按 ASCII 码表中字符形式进行的，但它实际上最终传输的还是一个字节数据。这个表格，当然不需要大家去记住，理解它，用的时候过来查就行了。</p>
<p>通信的学习，不像前边控制部分那么直观了，通信部分我们的程序只能获得一个结果，而其过程我们却无法直接看到，所以慢慢的可能大家就会知道有示波器和逻辑分析仪这类测量仪器。如果学校实验室或者公司里有示波器或者逻辑分析仪这类仪器，可以拿过来抓一下串口波形，直观的了解一下。如果暂时还没有这些仪器，先知道这么回事，有条件再说。因为工具类设备有的比较昂贵，有条件可以尽量使用学校或者公司的。在这里我用一款简易的逻辑分析仪把串口通信的波形抓出来给大家看一下，大家了解一下即可，如图 11-7 所示。</p>
<p><img src="http://c.biancheng.net/cpp/uploads/allimg/140318/1-14031Q6234Q96.png" alt></p>
<center><strong>图 11-7  逻辑分析仪串口数据示意图</strong></center>

<p>分析仪和示波器的作用，就是把通信过程的波形抓出来进行分析。先大概说一下波形的意思。波形左边是低位，右边是高位，上边这个波形是电脑发送给单片机的，下边这个波形是单片机回发给电脑的。以上边的波形为例，左边第一位是起始位 0，从低位到高位依次是10001100，顺序倒一下，就是数据 0x31，也就是 ASCII 码表里的‘1’。大家可以注意到分析仪在每个数据位都给标了一个白色的点，表示是数据，起始位和无数据的时候都没有这个白点。时间标 T1 和 T2 的差值在右边显示出来是 0.102ms，大概是 9600 分之一，稍微有点偏差，在容许范围内即可。通过图 11-7，我们可以清晰的了解了串口通信的收发的详细过程。</p>
<p>那我们这里再来了解一下，如果我们使用串口调试助手，用字符格式直接发送一个“12”，我们在我们的数码管上应该显示什么呢？串口调试助手应该返回什么呢？经过试验发现，我们数码管显示的是 32，而串口调试助手返回十六进制显示的是 31、32 两个数据，如图 11-8所示。</p>
<p><img src="http://c.biancheng.net/cpp/uploads/allimg/140318/1-14031Q62503637.png" alt></p>
<center><strong>图 11-8  串口调试助手数据显示</strong></center>

<p>我们再用逻辑分析仪把这个数据抓出来看一下，如图 11-9 所示。</p>
<p><img src="http://c.biancheng.net/cpp/uploads/allimg/140318/1-14031Q6255AB.png" alt></p>
<center><strong>图 11-9  逻辑分析仪抓取数据</strong></center>

<p>对于 ASCII 码表来说，数字本身是字符而非数据，所以如果发送“12”的话，实际上是是分别发送了“1”和“2”两个字符，单片机呢，先收到第一个字符“1”，在数码管上会显示出 31 这个对应数字，但是马上就又收到了“2”这个字符，数码管瞬间从 31 变成了 32，而我们视觉上呢，是没有办法发现这种快速变化的，所以我们感觉数码管直接显示的是 32。</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/limh/tags/教程/" rel="tag"># 教程</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/limh/2014/12/01/mcu074/" rel="next" title="11.5、UART串口通信的基本应用">
                <i class="fa fa-chevron-left"></i> 11.5、UART串口通信的基本应用
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/limh/2014/12/01/mcu076/" rel="prev" title="12.1、C语言变量的地址">
                12.1、C语言变量的地址 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="SOHUCS"></div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      

      <section class="site-overview-wrap sidebar-panel sidebar-panel-active">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image" src="/limh/images/avatar.jpg" alt="土逗牛肉">
            
              <p class="site-author-name" itemprop="name">土逗牛肉</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/limh/archives/">
              
                  <span class="site-state-item-count">255</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/limh/categories/index.html">
                  <span class="site-state-item-count">8</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/limh/tags/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/limhGeek" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:1591054297@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="https://www.jianshu.com/u/f73b243e7cc9" target="_blank" title="简书">
                      
                        <i class="fa fa-fw fa-user-circle-o"></i>简书</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">土逗牛肉</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
	var str = GetUrlParam('height');
    if (str === '0'){
		document.getElementById('header').style.display = 'none';
		document.getElementById('comments').style.display = 'none';
		document.getElementById('footer').style.display = 'none';
		document.getElementsByClassName('post-footer').item(0).style.display = 'none'
	}
	
    function GetUrlParam(paraName) {
        var url = document.location.toString();
        var arrObj = url.split("?");

        if (arrObj.length > 1) {
            var arrPara = arrObj[1].split("&");
            var arr;

            for (var i = 0; i < arrPara.length; i++) {
                 arr = arrPara[i].split("=");

                if (arr != null && arr[0] === paraName) {
                    return arr[1];
                }
            }
            return "";
        } else {
            return "";
        }
    }
</script>









  












  
  
    <script type="text/javascript" src="/limh/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/limh/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/limh/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/limh/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/limh/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/limh/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/limh/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/limh/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
